package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.service.FunctionServer;
import com.campus.app.service.StudentsService;
import com.campus.commons.entity.Counsellor;
import com.campus.app.mapper.CounsellorMapper;
import com.campus.app.service.CounsellorsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 助教团辅导班级表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class CounsellorServiceImpl extends ServiceImpl<CounsellorMapper, Counsellor> implements CounsellorsService {

    @Autowired
    private CounsellorMapper counsellorMapper;
    @Autowired
    private FunctionServer functionServer;
    @Autowired
    private StudentsService studentsService;

    @Override
    public Result<List<Counsellor>> getAllCounsellor(int current, int size) {
        return Result.result(counsellorMapper.selectPage(new Page<Counsellor>(current,size),null));
    }

    @Override
    public Result upCounsellorVerify(Counsellor counsellor) {
        //获取最新数据
        FunctionServerImpl.init();
//        验证 验证名字
        Integer classroomId = FunctionServerImpl.classRooms.get(counsellor.getClassroomName());
        String msg = classroomId == null ? "没有该教室的资料" : "";
        Integer classId = FunctionServerImpl.classes.get(counsellor.getClassName());
        msg += classId == null ? "没有该班级的资料" : "";
        Integer studentId = studentsService.existStudentByNameFromDepend(counsellor.getStudentName());
        msg += studentId == null ? "没有该学生的资料" : "";
        if (msg.trim().length() != 0) {
            return Result.error(Error.Operation_E1R, msg);
        } else {
            counsellor.setStudentid(studentId);
            counsellor.setClassid(classId);
            counsellor.setClassroomid(classroomId);
        }
        return upCounsellor(counsellor);
    }

    @Override
    public Result<List<Counsellor>> getCounsellors(int current, int size, Date start, Date end, String className) {
        QueryWrapper<Counsellor> queryWrapper = new QueryWrapper();
        String starts = Tool.getDateToString(start);
        String ends = Tool.getDateToString(end);
        queryWrapper.inSql(className!=null,"classId","(select c.id from class c where c.className like '%"+className+"%')");
        queryWrapper.between(start!=null,"Date(date)", starts, ends);
        return Result.result(counsellorMapper.selectPage(new Page<Counsellor>(current,size),queryWrapper));
    }

    @Override
    public Result addCounsellor(@NotNull Counsellor counsellor) {
        return Result.result(counsellorMapper.insert(counsellor));
    }

    @Override
    public Result delCounsellor(@Min(1) Integer counsellorId) {
        return Result.result(counsellorMapper.deleteById(counsellorId));
    }

    @Override
    public Result upCounsellor(@NotNull Counsellor counsellor) {
//        验证；
        return Result.result(counsellorMapper.updateById(counsellor));
    }

    @Override
    public Result upLoadCounsellors(InputStream file, String fileName) {
        try {
            List<Counsellor> counsellors = functionServer.upLoadCounsellor(file, fileName);
            //插入；
            int i = counsellorMapper.addCounsellors(counsellors);
            return Result.result(counsellors.size(), i);
        } catch (IOException e) {
            e.printStackTrace();
          return   Result.error(Error.File_E2R,null );
        }
    }
}
